package org.xtframe.service.impl;

import java.util.List;

import net.sf.json.JSONArray;

import org.xtframe.drools.business.BusinessExecutor;
import org.xtframe.drools.vo.BusinessResult;
import org.xtframe.entity.SQL;
import org.xtframe.entity.ServerData;
import org.xtframe.entity.UserData;
import org.xtframe.sql.SqlData;
import org.xtframe.util.CommonUtil;
import org.xtframe.util.StringUtil;
import org.xtframe.vo.CheckVO;

public abstract class BasicExecute {

	public CheckVO check(String sqlID, String arrValue, UserData userData, ServerData serverData) throws Exception{
		CheckVO checkVO = new CheckVO();
		if(StringUtil.isNullStr(sqlID)){
			checkVO.setResult(CheckVO.SQLID_NULL);	// sqlID为空
			return checkVO;
		}
		
		BusinessResult bresult = BusinessExecutor.verifySQLExecutivePower(sqlID);
		if(bresult.getResult() != BusinessResult.SUCCESS){
			checkVO.setResult(CheckVO.SQLID_NO_RIGHT);	// 没有该sql的执行权限
			return checkVO;
		}
		
		SQL sql = serverData.getSql(sqlID);
		if(sql == null){
			checkVO.setResult(CheckVO.SQLID_INEXISTENCE);	// 该sql不存在
			return checkVO;
		}
		
		JSONArray array = CommonUtil.getJSONArray(arrValue);
		List<String> parameters = CommonUtil.JSONArrayToArrayList_String(array);
		
		//判断数据格式是否符合要求
		SqlData sqlData = sql.getDataVerify().createSql(sql.getSqlMode(), parameters);
		if(!sqlData.isResult()) {
			checkVO.setResult(CheckVO.CHECK_FAILED);
			return checkVO;
		}
		checkVO.setSqlData(sqlData);
		checkVO.setResult(CheckVO.SUCCESS);
		return checkVO;
	}
}
